package com.sun.dailyprj.provider;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.sun.dailyprj.util.Logger;


public class LogService extends Service{

    private static final String NOTIFY_CHANNEL_ID = "Chery";
    private LogServer mLogServer;

    public static void startService(Context context, Intent intent) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notiChannel = new NotificationChannel(
                    NOTIFY_CHANNEL_ID, "CORE", NotificationManager.IMPORTANCE_NONE);
            notiChannel.setShowBadge(false);
            NotificationManager notiMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notiMgr.createNotificationChannel(notiChannel);
        }

        Notification noti;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            noti = new Notification.Builder(this, NOTIFY_CHANNEL_ID).build();
        } else {
            noti = new Notification.Builder(this).build();
        }
        startForeground(1, noti);

        mLogServer = new LogServer();
        mLogServer.initLogServer();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Logger.warn("attachBaseContext()");
    }


}
